
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * First exercises to get acquainted with the GeoTOP file format
 * @author kooijmanj1
 *
 */
public class Speel {
	public static final String KOMMA = ",";
	public static final String INFILE = "C:/Data/GeoTOP/voxels_denbosch.csv";
	public static final String OUTFILE = "C:/Data/GeoTOP/voxels_denbosch_roundtrip.txt";


	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int nx = 20;
		int ny = 30;
		int nz = 320;
		BufferedReader reader = null;
		PrintWriter writer = null;
		try{ 
			String[] metaData = new String[12];
			reader = new BufferedReader(new FileReader(new File(INFILE)));
			writer =  new PrintWriter(new BufferedWriter(new FileWriter(new File(OUTFILE))));
			long startTime = System.currentTimeMillis();
			int lineNumber = 0;
			String line = reader.readLine();
			metaData[lineNumber] = line;
			String[] gridType = metaData[0].split("",2);
			writer.println("gridType: " + gridType[0] + " " + gridType[1]);
			lineNumber++;
			while (line != null && lineNumber < 12){
				metaData[lineNumber] = reader.readLine();
				lineNumber++;
			}
			for (String metaDataItem : metaData){
				writer.println(metaDataItem);
			}
			// meanings line
			line = reader.readLine();
			if (line != null){
				String[] meanings = line.split(KOMMA);
				String lineMeanings = "";
				for (String meaning : meanings){
					lineMeanings = lineMeanings + meaning + KOMMA;
				}
				int length = lineMeanings.length();
				lineMeanings = lineMeanings.substring(0, length-1);
				writer.println("meanings: " + lineMeanings);
			}
			// types line
			line =reader.readLine();
			if(line != null){
				String[] types = line.split(KOMMA);
				String lineTypes ="";
				for (String type : types){
					lineTypes = lineTypes + type + KOMMA;
				}
				int length = lineTypes.length();
				lineTypes = lineTypes.substring(0, length - 1);
				writer.println("types: " + lineTypes);
			}

			writer.println("Now the real data");
			
			Double[][][] matrixKlasse = new Double[nx][ny][nz];
			Double[][][] matrixLitho = new Double[nx][ny][nz];
			Double[][][] matrixKans1 = new Double[nx][ny][nz];
			lineNumber = 0;
			for (int x = 0; x < nx; x++){
				for (int y = 0; y < ny; y++){
					for (int z = 0; z< nz ; z++){
						line = reader.readLine();
						String[] lineValues = line.split(KOMMA);
						matrixKlasse[x][y][z] = Double.parseDouble(lineValues[0]);
						matrixLitho[x][y][z] = Double.parseDouble(lineValues[1]);
						matrixKans1[x][y][z] = Double.parseDouble(lineValues[2]);
				lineNumber++;
					}
				}
			}
			
			// for roundtripping
			for (int x = 0; x < nx; x++){
				for (int y = 0; y < ny; y++){
					for (int z = 0; z< nz ; z++){
						// om het bovenste gedeelte te lezen
						writer.println("Waarden " + (1+z+nz*y + x*nz*ny) + " " + matrixKlasse[x][y][z] + " " 
								+ matrixLitho[x][y][z] + " " + matrixKans1[x][y][z]);
						// om het onderste deel te lezen
						System.out.println("Waarden " + (1+z+nz*y + x*nz*ny) + " " + matrixKlasse[x][y][z]
						+ " "  + matrixLitho[x][y][z] + " " + matrixKans1[x][y][z]);
					} 
				}
			}
			
			writer.println("number of elements in matrix: " + matrixKlasse.length);// how to count 3D matrix?
			
			long endTime = System.currentTimeMillis();
			System.out.println("Time in Millis used for reading: " + (endTime-startTime));
		}
		catch(FileNotFoundException e){
			System.out.println(e.getMessage());
		}
		catch(IOException e){
			System.out.println(e.getMessage());
		}
		finally {
			if(reader != null){
				try{
					reader.close();
				}
				catch (IOException e){}
			}
		}		
	}
	
}
